/*
 * RoleValueDisplay.java
 *
 * Created on 20 January 2004, 10:48
 * Copyright (C) 2004, Velocityme Partnership
 */

package com.velocityme.client.gui.node.role;

import com.velocityme.valueobjects.NodeValue;
import com.velocityme.valueobjects.RoleValue;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 *
 * @author  Robert Crida Work
 */
public class RoleValueDisplay extends javax.swing.JPanel {
    
    private RoleValue m_roleValue;
    private PermissionTableModel m_modelPermission;
    
    /** Creates new form RoleValueDisplay */
    public RoleValueDisplay(RoleValue p_roleValue, Set p_assignedPermissionValues, Set p_allPermissionValues) {
        initComponents();
        setRoleValue(p_roleValue, p_assignedPermissionValues, p_allPermissionValues);
    }
    
    public void setRoleValue(RoleValue p_roleValue, Set p_assignedPermissionValues, Set p_allPermissionValues) {
        NodeValue nodeValue = p_roleValue.getNodeValue();
        if (nodeValue != null) {
            if (nodeValue.nameHasBeenSet())
                jTextFieldName.setText(nodeValue.getName());
            if (nodeValue.descriptionHasBeenSet())
                jTextAreaDescription.setText(nodeValue.getDescription());
        }
        if (p_roleValue.isRecursiveHasBeenSet())
            jCheckBoxRecursive.setSelected(p_roleValue.getIsRecursive().booleanValue());
        Map permissionMap = new TreeMap();
        Iterator i = p_allPermissionValues.iterator();
        while (i.hasNext()) {
            Object permission = i.next();
            permissionMap.put(permission, new Boolean(p_assignedPermissionValues.contains(permission)));
        }
        m_modelPermission.setValues(permissionMap);
        m_roleValue = p_roleValue;
    }
    
    public RoleValue getRoleValue() {
        if (m_roleValue.getNodeValue() == null)
            m_roleValue.setNodeValue(new NodeValue());
        m_roleValue.setIsRecursive(new Boolean(jCheckBoxRecursive.isSelected()));
        m_roleValue.getNodeValue().setName(jTextFieldName.getText());
        m_roleValue.getNodeValue().setDescription(jTextAreaDescription.getText());
        return m_roleValue;
    }
    
    public Set getAssignedPermissionValues() {
        return m_modelPermission.getAssignedValues();
    }
    
    public void setEditable(boolean p_editable) {
        jTextFieldName.setEditable(p_editable);
        jTextAreaDescription.setEditable(p_editable);
        jCheckBoxRecursive.setEnabled(p_editable);
        m_modelPermission.setEditable(p_editable);
    }
    
   /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        java.awt.GridBagConstraints gridBagConstraints;

        jLabelIcon = new javax.swing.JLabel();
        jLabelName = new javax.swing.JLabel();
        jTextFieldName = new javax.swing.JTextField();
        Description = new javax.swing.JLabel();
        jScrollPaneDescription = new javax.swing.JScrollPane();
        jTextAreaDescription = new javax.swing.JTextArea();
        jLabelRecursive = new javax.swing.JLabel();
        jCheckBoxRecursive = new javax.swing.JCheckBox();
        jLabelPermissions = new javax.swing.JLabel();
        jScrollPanePermissions = new javax.swing.JScrollPane();
        m_modelPermission = new PermissionTableModel();
        jTable1 = new javax.swing.JTable(m_modelPermission);

        setLayout(new java.awt.GridBagLayout());

        jLabelIcon.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/velocityme/client/icons/role-32x32.png")));
        add(jLabelIcon, new java.awt.GridBagConstraints());

        jLabelName.setText("Name:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        add(jLabelName, gridBagConstraints);

        jTextFieldName.setEditable(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.weightx = 1.0;
        add(jTextFieldName, gridBagConstraints);

        Description.setText("Description:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        add(Description, gridBagConstraints);

        jTextAreaDescription.setEditable(false);
        jTextAreaDescription.setLineWrap(true);
        jTextAreaDescription.setWrapStyleWord(true);
        jScrollPaneDescription.setViewportView(jTextAreaDescription);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.weighty = 0.5;
        add(jScrollPaneDescription, gridBagConstraints);

        jLabelRecursive.setText("Recursive:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        add(jLabelRecursive, gridBagConstraints);

        jCheckBoxRecursive.setEnabled(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        add(jCheckBoxRecursive, gridBagConstraints);

        jLabelPermissions.setText("Permissions:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        add(jLabelPermissions, gridBagConstraints);

        jScrollPanePermissions.setViewportView(jTable1);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        gridBagConstraints.weighty = 1.0;
        add(jScrollPanePermissions, gridBagConstraints);

    }//GEN-END:initComponents
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel Description;
    private javax.swing.JCheckBox jCheckBoxRecursive;
    private javax.swing.JLabel jLabelIcon;
    private javax.swing.JLabel jLabelName;
    private javax.swing.JLabel jLabelPermissions;
    private javax.swing.JLabel jLabelRecursive;
    private javax.swing.JScrollPane jScrollPaneDescription;
    private javax.swing.JScrollPane jScrollPanePermissions;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextArea jTextAreaDescription;
    private javax.swing.JTextField jTextFieldName;
    // End of variables declaration//GEN-END:variables
    
}
